package com.zhilun.device;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhilun.device.entity.DeviceBindSensorVm;
import com.zhilun.device.entity.DeviceBindSensorVmAll;
import com.zhilun.device.entity.DeviceTyreLocationSensorParam;
import com.zhilun.device.entity.TireData;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeviceBindRequest {


    static Map<String, Integer> translationLocation = new HashMap<>();
    static {

        translationLocation.put("备3",97);
        translationLocation.put("备1",98);
        translationLocation.put("备2",99);
    }

    public static void main(String[] args) {
        // 设置 URL
        //pro
        String url = "https://www.56dache.com/dache-wmms-admin/handler/deviceBindSensor/saveBindDeviceSensorAll.create";

//        String url = "localhost:8080/dache-wmms-admin/handler/deviceBindSensor/saveBindDeviceSensorAll.create";

        //String filePath = "C:\\Users\\hcf\\Downloads\\传感器-知轮-需推大车-1128.xlsx";  // 替换为实际文件路径
        String filePath = "C:\\Users\\hcf\\Desktop\\传感器-知轮-需推大车-1128.xlsx";  // 替换为实际文件路径

        // 读取 Excel 文件
        List<TireData> dataList = EasyExcel.read(filePath)
                .head(TireData.class)
                .sheet().headRowNumber(1)
                .doReadSync();

        int count =0;
        for (TireData data : dataList) {
            if (StrUtil.isBlank(data.getDeviceId()) ||StrUtil.isBlank(data.getLicensePlate())) {
                continue;
            }
            DeviceBindSensorVmAll deviceBindSensorVmAll = new DeviceBindSensorVmAll();
            DeviceBindSensorVm deviceBindSensorVm = new DeviceBindSensorVm();
            deviceBindSensorVm.setDeviceId(data.getDeviceId());
            deviceBindSensorVm.setLicensePlate(data.getLicensePlate());
            DeviceTyreLocationSensorParam param = new DeviceTyreLocationSensorParam();
            param.setSensorId(data.getSensorId());

            param.setTyreLocationId(data.getLocation());

            deviceBindSensorVm.setTyreLocationInfo(Collections.singletonList(param));

            deviceBindSensorVmAll.setDeviceBindSensorVm(deviceBindSensorVm);
            deviceBindSensorVmAll.setDeviceBindAxleSensorVm(new DeviceBindSensorVm());
            // 创建 JSON 请求体
            String requestBody = JSON.toJSONString(deviceBindSensorVmAll);

            post(url, requestBody,data.getSensorId());
            count++;
            if (count ==2) {
                return;
            }
        }



    }

    private static void post(String url, String requestBody, String sensorId) {
        // 发送 POST 请求
        HttpResponse response = HttpRequest.post(url)
            .header("accept", "application/json, text/plain, */*")
            .header("accept-language", "zh-CN,zh;q=0.9")
            .header("cache-control", "no-cache")
            .header("content-type", "application/json;charset=UTF-8")
            .header("cookie", "JSESSIONID=54c3c82a-b44c-4c7f-a481-c085be65d313; SERVERID=17db52a41f1405e932d014be0c02fb21|1732792861|1732702746")
            .header("origin", "https://www.56dache.com")
            .header("pragma", "no-cache")
            .header("priority", "u=1, i")
            .header("referer", "https://www.56dache.com/dache-wmms-admin/")
            .header("sec-ch-ua", "\"Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99\"")
            .header("sec-ch-ua-mobile", "?0")
            .header("sec-ch-ua-platform", "\"Windows\"")
            .header("sec-fetch-dest", "empty")
            .header("sec-fetch-mode", "cors")
            .header("sec-fetch-site", "same-origin")
            .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")
            .body(requestBody)
            .execute();

        // 输出响应
        System.out.println(response.body());
        JSONObject jsonObject = JSON.parseObject(response.body());
        if (!jsonObject.getString("msg").equals("成功")) {
            System.out.println(sensorId);
            System.out.println(response.body());
        }
    }
}
